#include "mp.h"
#include "string.h"


//struct
//{
//	uint32_t extid;
//	uint32_t maskid;
//	
//}mask_filter;

//void mask(void)
//{
//	mask_filter.maskid=0xFF00FFF0;
//	mask_filter.extid=0x1002FFA7
//}
struct
{
	uint8_t main_hardware_version;
	uint8_t sub_hardware_version;
	uint8_t main_software_version;
	uint8_t sub_software_version;
	uint8_t boot1_softver;
	uint8_t boot2_softver;
	uint8_t main_comver;
	uint8_t sub_comver;
}bms_ver_info;

struct
{
	uint8_t productver;
	uint8_t hardware_factory_code;
	uint16_t customer_code;
	uint16_t design_factory;
	uint16_t reserve;
}bms_factory_info;

struct
{
	uint8_t outyear;
	uint8_t outmonth;
	uint8_t outday;
	uint8_t reserve[5];
}bms_output_day;

struct
{
	uint8_t SN0;
	uint8_t SN1;
	uint8_t SN2;
	uint8_t SN3;
	uint32_t reserve;
}bms_SN_code;

struct
{
	uint16_t battery_vol;
	uint8_t battery_load;
	uint8_t battery_core_S;
	uint8_t battery_core_P;
	uint8_t battery_kind;
	uint16_t reserve;
}bms_para;

struct
{
	uint8_t battery_0_1;
	uint8_t battery_2_3;
	uint8_t battery_4_5;
	uint8_t battery_6_7;
	uint8_t serbattery;
	uint8_t reserve[3];
}bms_workstatus;

struct
{
	uint16_t general_bat_vol;
	uint16_t general_bat_cur;
	uint16_t timetofull;
	uint16_t reserve;
}general_battery_info;

struct
{
	uint16_t gen_moment_max_out_cur;
	uint16_t gen_con_max_out_cur;
	uint16_t gen_moment_max_in_cur;
	uint16_t gen_callback_max_cur;
}bms_SOP_info;

struct
{
	uint16_t battery_vol;
	uint16_t battery_cur;
	uint8_t MOSstatus;
	uint8_t reserve1;
	uint8_t flagstatus;
	uint8_t reserve2;
}bms_vol_MOS;

struct
{
	uint16_t bat_moment_max_out_cur;
	uint16_t bat_moment_max_in_cur;
	uint32_t reserve;
}bms_moment_cur;

struct
{
	uint16_t battery_p_insu_res;
	uint16_t battery_n_insu_res;
	uint32_t reserve;
}bms_insulation_info;

struct
{
	uint16_t SOC;
	uint16_t SOH;
	uint16_t cycle_times;
	uint16_t reserve;
}bms_SOX_info;

struct
{
	uint16_t battery_fullload;
	uint16_t battery_leftload;
	uint16_t SOE;
	uint16_t reach_full_time;
}bms_left_info;

struct
{
	uint16_t battery_core_min_vol;
	uint16_t battery_core_max_vol;
}bms_single_info;

struct
{
	uint16_t battery_in_cur;
	uint16_t battery_callback_cur;
	uint16_t battery_out_cur;
}bms_con_cur_info;

struct
{
	uint8_t battery_temp_max;
	uint8_t battery_temp_min;
	uint8_t battery_MOS_temp;
	uint8_t battery_env_temp;
}bms_temp_info;

struct
{
	unsigned int single_overvol:2;
	unsigned int single_leakvol:2;
	unsigned int general_overvol:2;
	unsigned int general_leakvol:2;
	unsigned int powerout_overtemp:2; 
	unsigned int powerout_lowtemp:2;
	unsigned int powerin_overtemp:2;
	unsigned int powerin_lowtemp:2;
	unsigned int tempchange:2;
	unsigned int powerin_overcur:2;
	unsigned int callback_overcur:2;
	unsigned int powerout_overcur:2;
	unsigned int powerout_moment_overcur:2;
	unsigned int low_insulation:2;
	unsigned int low_SOC:2;
	unsigned int Bat_id_differ:2;
	unsigned int AFE_comu_wrong:1;
	unsigned int vol_collect_wrong:1;
	unsigned int temp_collect_wrong:1;
	unsigned int balance_overtemp:1;
	unsigned int mos_overtemp:1;
	unsigned int pre_charge_res_overtemp:1;
	unsigned int pre_charge_timeout:1;
	unsigned int pre_charge_overcur:1;
	unsigned int AFE_overvol:1;
	unsigned int AFE_leakvol:1;
	unsigned int AFE_powerout_overcur:1;
	unsigned int AFE_charge_overcur:1;
	unsigned int AFE_short_out:1;
	unsigned int AFE_charge_overtemp:1;
	unsigned int AFE_charge_lowtemp:1;
	unsigned int AFE_powerout_overtemp:1;
	unsigned int AFE_powerout_lowtemp:1;
	unsigned int floatcharger:1;
	unsigned int ban_charge_0v:1;
	unsigned int pre_powerout_mos_adhension:1;
	unsigned int powerout_mos_adhension:1;
	unsigned int charge_mos_adhension:1;
	unsigned int pre_powerout_mos_wrong:1;
	unsigned int powerout_mos_wrong:1;
	unsigned int charge_mos_wrong:1;
	unsigned int EOL:1;
	unsigned int bat_overvol_heavy:1;
	unsigned int charge_protect_status:1;
	unsigned int powerout_protect_status:1;
}bms_error_info;

struct
{
	uint8_t bms_vol[8];
}bms_vol_info;

struct
{
	uint8_t bms_temp[8];
}bms_singletemp_info;

struct
{
	uint8_t bms_realtime[8];
}bms_realtime_info;

void bms_ver_info_fun(can_receive_message_struct *version)
{
	bms_ver_info.main_hardware_version=version->rx_data[0];
	bms_ver_info.sub_hardware_version=version->rx_data[1];
	bms_ver_info.main_software_version=version->rx_data[2];
	bms_ver_info.sub_software_version=version->rx_data[3];
	bms_ver_info.boot1_softver=version->rx_data[4];
	bms_ver_info.boot2_softver=version->rx_data[5];
	bms_ver_info.main_comver=version->rx_data[6];
	bms_ver_info.sub_comver=version->rx_data[7];
}

void bms_factory_info_fun(can_receive_message_struct *factory)
{
	bms_factory_info.productver=factory->rx_data[0];
	bms_factory_info.hardware_factory_code=factory->rx_data[1];
	bms_factory_info.customer_code=factory->rx_data[3]<<8|factory->rx_data[2];
	bms_factory_info.design_factory=factory->rx_data[5]<<8|factory->rx_data[4];
}

void bms_output_day_fun(can_receive_message_struct *output)
{
	bms_output_day.outyear=output->rx_data[0];
	bms_output_day.outmonth=output->rx_data[1];
	bms_output_day.outday=output->rx_data[2];
}

void bms_SN_code_fun(can_receive_message_struct *SN)
{
	bms_SN_code.SN0=SN->rx_data[0];
	bms_SN_code.SN1=SN->rx_data[1];
	bms_SN_code.SN2=SN->rx_data[2];
	bms_SN_code.SN3=SN->rx_data[3];
}

void bms_para_fun(can_receive_message_struct *para)
{
	bms_para.battery_vol=para->rx_data[1]<<8|para->rx_data[0];
	bms_para.battery_load=para->rx_data[2];
	bms_para.battery_core_S=para->rx_data[3];
	bms_para.battery_core_P=para->rx_data[4];
	bms_para.battery_kind=para->rx_data[5];
}

void bms_workstatus_fun(can_receive_message_struct *workstatus)
{
	bms_workstatus.battery_0_1=workstatus->rx_data[0];
	bms_workstatus.battery_2_3=workstatus->rx_data[1];
	bms_workstatus.battery_4_5=workstatus->rx_data[2];
	bms_workstatus.battery_6_7=workstatus->rx_data[3];
	bms_workstatus.serbattery=workstatus->rx_data[4];
}

void general_battery_info_fun(can_receive_message_struct *general)
{
	general_battery_info.general_bat_vol=(general->rx_data[1]<<8)|general->rx_data[0];
	general_battery_info.general_bat_cur=((general->rx_data[3])<<8)|general->rx_data[2];
	general_battery_info.timetofull=((general->rx_data[5])<<8)|general->rx_data[4];
}

void bms_SOP_info_fun(can_receive_message_struct *SOP)
{
	bms_SOP_info.gen_moment_max_out_cur=SOP->rx_data[1]|SOP->rx_data[0];
	bms_SOP_info.gen_con_max_out_cur=SOP->rx_data[3]|SOP->rx_data[2];
	bms_SOP_info.gen_moment_max_in_cur=SOP->rx_data[5]|SOP->rx_data[4];
	bms_SOP_info.gen_callback_max_cur=SOP->rx_data[7]|SOP->rx_data[6];
}

void bms_vol_MOS_fun(can_receive_message_struct *MOS_vol)
{
	bms_vol_MOS.battery_vol=MOS_vol->rx_data[1]<<8|MOS_vol->rx_data[0];
	bms_vol_MOS.battery_cur=MOS_vol->rx_data[3]<<8|MOS_vol->rx_data[2];
	bms_vol_MOS.MOSstatus=MOS_vol->rx_data[4];
	bms_vol_MOS.flagstatus=MOS_vol->rx_data[6];
}

void bms_moment_cur_fun(can_receive_message_struct *moment_cur)
{
	bms_moment_cur.bat_moment_max_out_cur=moment_cur->rx_data[1]<<8|moment_cur->rx_data[0];
	bms_moment_cur.bat_moment_max_in_cur=moment_cur->rx_data[3]<<8|moment_cur->rx_data[2];
}

void bms_insulation_info_fun(can_receive_message_struct *insulation_info)
{
	bms_insulation_info.battery_p_insu_res=insulation_info->rx_data[1]<<8|insulation_info->rx_data[0];
	bms_insulation_info.battery_n_insu_res=insulation_info->rx_data[3]<<8|insulation_info->rx_data[2];
}

void bms_SOX_info_fun(can_receive_message_struct *SOX_info)
{
	bms_SOX_info.SOC=SOX_info->rx_data[1]<<8|SOX_info->rx_data[0];
	bms_SOX_info.SOH=SOX_info->rx_data[3]<<8|SOX_info->rx_data[2];
	bms_SOX_info.cycle_times=SOX_info->rx_data[5]<<8|SOX_info->rx_data[4];
}

void bms_left_info_fun(can_receive_message_struct *left_info)
{
	bms_left_info.battery_fullload=left_info->rx_data[1]<<8|left_info->rx_data[0];
	bms_left_info.battery_leftload=left_info->rx_data[3]<<8|left_info->rx_data[2];
	bms_left_info.SOE=left_info->rx_data[5]<<8|left_info->rx_data[4];
	bms_left_info.reach_full_time=left_info->rx_data[7]<<8|left_info->rx_data[6];
}

void bms_single_info_fun(can_receive_message_struct *single_info)
{
	bms_single_info.battery_core_min_vol=single_info->rx_data[1]<<8|single_info->rx_data[0];
	bms_single_info.battery_core_max_vol=single_info->rx_data[3]<<8|single_info->rx_data[2];
}

void bms_con_cur_info_fun(can_receive_message_struct *con_cur)
{
	bms_con_cur_info.battery_in_cur=con_cur->rx_data[1]<<8|con_cur->rx_data[0];
	bms_con_cur_info.battery_callback_cur=con_cur->rx_data[3]<<8|con_cur->rx_data[2];
	bms_con_cur_info.battery_out_cur=con_cur->rx_data[5]<<8|con_cur->rx_data[4];
}

void bms_temp_info_fun(can_receive_message_struct *temp)
{
	bms_temp_info.battery_temp_max=temp->rx_data[0];
	bms_temp_info.battery_temp_min=temp->rx_data[1];
	bms_temp_info.battery_MOS_temp=temp->rx_data[2];
	bms_temp_info.battery_env_temp=temp->rx_data[3];
}

void bms_error_info_fun(can_receive_message_struct *error_info)
{
	bms_error_info.single_overvol=error_info->rx_data[0]&0x03;
	bms_error_info.single_leakvol=error_info->rx_data[0]&0x0c;
	bms_error_info.general_overvol=error_info->rx_data[0]&0x30;
	bms_error_info.general_leakvol=error_info->rx_data[0]&0xc0;
	bms_error_info.powerout_overtemp=error_info->rx_data[1]&0x03;
	bms_error_info.powerout_lowtemp=error_info->rx_data[1]&0x0c;
	bms_error_info.powerin_overtemp=error_info->rx_data[1]&0x30;
	bms_error_info.powerin_lowtemp=error_info->rx_data[1]&0xc0;
	bms_error_info.tempchange=error_info->rx_data[2]&0x03;
	bms_error_info.powerin_overcur=error_info->rx_data[2]&0x0c;
	bms_error_info.callback_overcur=error_info->rx_data[2]&0x30;
	bms_error_info.powerout_overcur=error_info->rx_data[2]&0xc0;
	bms_error_info.powerout_moment_overcur=error_info->rx_data[3]&0x03;
	bms_error_info.low_insulation=error_info->rx_data[3]&0x0c;
	bms_error_info.low_SOC=error_info->rx_data[3]&0x30;
	bms_error_info.Bat_id_differ=error_info->rx_data[3]&0xc0;
	bms_error_info.AFE_comu_wrong=error_info->rx_data[4]&0x01;
	bms_error_info.vol_collect_wrong=error_info->rx_data[4]&0x02;
	bms_error_info.temp_collect_wrong=error_info->rx_data[4]&0x04;
	bms_error_info.balance_overtemp=error_info->rx_data[4]&0x08;
	bms_error_info.mos_overtemp=error_info->rx_data[4]&0x10;
	bms_error_info.pre_charge_res_overtemp=error_info->rx_data[4]&0x20;
	bms_error_info.pre_charge_timeout=error_info->rx_data[4]&0x40;
	bms_error_info.pre_charge_overcur=error_info->rx_data[4]&0x80;
	bms_error_info.AFE_overvol=error_info->rx_data[5]&0x01;
	bms_error_info.AFE_leakvol=error_info->rx_data[5]&0x02;
	bms_error_info.AFE_powerout_overcur=error_info->rx_data[5]&0x04;
	bms_error_info.AFE_charge_overcur=error_info->rx_data[5]&0x08;
	bms_error_info.AFE_short_out=error_info->rx_data[5]&0x10;
	bms_error_info.AFE_charge_overtemp=error_info->rx_data[5]&0x20;
	bms_error_info.AFE_charge_lowtemp=error_info->rx_data[5]&0x40;
	bms_error_info.AFE_powerout_overtemp=error_info->rx_data[5]&0x80;
	bms_error_info.AFE_powerout_lowtemp=error_info->rx_data[6]&0x01;
	bms_error_info.floatcharger=error_info->rx_data[6]&0x02;
	bms_error_info.ban_charge_0v=error_info->rx_data[6]&0x04;
	bms_error_info.pre_powerout_mos_adhension=error_info->rx_data[6]&0x08;
	bms_error_info.powerout_mos_adhension=error_info->rx_data[6]&0x10;
	bms_error_info.charge_mos_adhension=error_info->rx_data[6]&0x20;
	bms_error_info.pre_powerout_mos_wrong=error_info->rx_data[6]&0x40;
	bms_error_info.EOL=error_info->rx_data[6]&0x80;
	bms_error_info.bat_overvol_heavy=error_info->rx_data[7]&0x01;
	bms_error_info.charge_protect_status=error_info->rx_data[7]&0x10;
	bms_error_info.powerout_protect_status=error_info->rx_data[7]&0x20;
	
}

void bms_vol_info_fun(can_receive_message_struct *vol_info)
{
	memcpy(&bms_vol_info.bms_vol,&vol_info->rx_data,8);
}

void bms_singletemp_info_fun(can_receive_message_struct *singletemp)
{
	memcpy(&bms_singletemp_info.bms_temp,&singletemp->rx_data,8);
}

void bms_realtime_info_fun(can_receive_message_struct *realtime_info)
{
	memcpy(&bms_realtime_info.bms_realtime,&realtime_info->rx_data,8);
}

